package com.cyan.app.workflow.fragment;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

import com.cyan.app.workflow.AppContext;
import com.cyan.app.workflow.AppData;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.activity.Wap.WebViewActivity;
import com.cyan.app.workflow.base.fragment.BaseFragment;
import com.cyan.app.workflow.bean.CommonBean;
import com.cyan.app.workflow.common.HttpConfig;
import com.cyan.app.workflow.customview.ShowTipDialog;
import com.cyan.app.workflow.util.AppUtils;
import com.cyan.app.workflow.util.network.GetRequestParams;
import com.google.gson.Gson;

import org.json.JSONObject;

/**
 * Created by owen on 2019/10/26
 */
public class SigFragment extends BaseFragment {
    public static final String TAG = "SigFragment";
    private TextView top_bar_middle;
    private WebView mWebView1;
    private ShowTipDialog showdialog = new ShowTipDialog();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        _RootView = inflater.inflate(R.layout.layout_report, null);


        return _RootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        mWebView1 = (WebView)_RootView.findViewById(R.id.web);

        mWebView1.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
             //   setProgress(newProgress*100);
            }
        });
        mWebView1.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
               /* Uri uri = Uri.parse(url);
                Intent viewIntent = new Intent(Intent.ACTION_VIEW,uri);
                startActivity(viewIntent);*/
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                String dataString = AppUtils.getFromAssets(AppContext.getInstance(), "404.html");
                view.loadDataWithBaseURL("404.html", dataString, "text/html", "UTF-8", "");
            }
        });
        mWebView1.getSettings().setJavaScriptEnabled(true);
        mWebView1.getSettings().setDomStorageEnabled(true);
        mWebView1.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

        mWebView1.getSettings().setSupportZoom(true);
        mWebView1.getSettings().setBuiltInZoomControls(true);

        mWebView1.getSettings().setUseWideViewPort(true);
        mWebView1.getSettings().setLoadWithOverviewMode(true);

        if (android.os.Build.VERSION.SDK_INT > 10) {
            mWebView1.getSettings().setDisplayZoomControls(false);
        }
        SigFragment.AsyncLoader task = new SigFragment.AsyncLoader();

       String weburl = HttpConfig.HTTP_MAIN_BASE_URL+"/login.aspx?returnUrl="+HttpConfig.HTTP_MAIN_BASE_URL+"/Main/workbench.aspx"+ "&UserNo=" + AppData.getInstance().getUserId()  + "&ConnectString=" + AppData.getInstance().getConnectstring() + "&FacNo=" + HttpConfig.Main_FacNo;
        Log.e("error", weburl);
        mWebView1.loadUrl(weburl);
       // task.execute("GetUrl", "GetSiglist");
    }

    public class AsyncLoader extends AsyncTask<String, Integer, String> {

        @Override
        protected void onPreExecute() {
           // showdialog.showDialog(SigFragment.this);
        }

        @Override
        protected String doInBackground(String... params) {
            String result = null;
            try{
                JSONObject param = new JSONObject();
                param.put("Type", params[0]);
                param.put("Code", params[1]);
                result= GetRequestParams.getRequestResult(HttpConfig.HTTP_MAIN_SERVER_URL, params[0], param.toString(), getActivity());
            }catch (Exception e){
                Log.e("error", e.toString());
            }
            return result;
        }

        @Override
        protected void onPostExecute(String s) {
            if (s == null||s.isEmpty()) {
               // showdialog.closeDialog();
                return;
            }
            CommonBean commonBean = new Gson().fromJson(s, CommonBean.class);
            if (commonBean.getCode() < 0) {
                //AppUtils.showTip(SigFragment.this, commonBean.getMsg());
            } else {
               String weburl = commonBean.getResult();
                weburl = HttpConfig.HTTP_MAIN_BASE_URL+weburl+ "&UserNo=" + AppData.getInstance().getUserId()  + "&ConnectString=" + AppData.getInstance().getConnectstring() + "&FactoryID=" + HttpConfig.Main_FactoryID;
                Log.e("error", weburl);
                mWebView1.loadUrl(weburl);
            }
            //showdialog.closeDialog();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    private void initView(){
        top_bar_middle = (TextView) _RootView.findViewById(R.id.top_bar_middle);
        top_bar_middle.setText(R.string.icon_sig);
    }
}
